/*
 * @author: wizardc
 */

using Dou.UI;
using TMPro;
using UnityEngine;

namespace Dou.ScreenController
{
    /// <summary>
    /// TextMeshPro 文本支持横竖屏切换的适配器
    /// </summary>
    [RequireComponent(typeof(DTextMeshProUGUI))]
    public class DTextMeshProUGUIAdapter : MonoBehaviour, IUIAdapter
    {
        [HideInInspector]
        public UIAdapterObject fontSetting;
        [HideInInspector]
        public UIAdapterFloat fontSize;
        [HideInInspector]
        public UIAdapterEnum<TextOverflowModes> overflowMode;
        
        private DTextMeshProUGUI _textMeshProUGUI;
        
        void Awake()
        {
            _textMeshProUGUI = GetComponent<DTextMeshProUGUI>();
            ScreenUIAdapterMgr.instance.AddAdapter(this);
        }
        
        public void RefreshAdapter(int uiScreenMode, int uiFineTuningMode)
        {
            if (fontSetting.GetValue(uiScreenMode, uiFineTuningMode, out var resultFontSetting))
            {
                _textMeshProUGUI.fontSetting = (FontSetting) resultFontSetting;
            }
            if (fontSize.GetValue(uiScreenMode, uiFineTuningMode, out var resultFontSize))
            {
                _textMeshProUGUI.fontSize = resultFontSize;
            }
            if (overflowMode.GetValue(uiScreenMode, uiFineTuningMode, out var resultFontOverflowMode))
            {
                _textMeshProUGUI.overflowMode = resultFontOverflowMode;
            }
        }

#if UNITY_EDITOR

        public void RefreshAdapterInEditor(int uiScreenMode, int uiFineTuningMode)
        {
            var text = GetComponent<DTextMeshProUGUI>();
            if (fontSetting.GetValue(uiScreenMode, uiFineTuningMode, out var resultFontSetting))
            {
                text.fontSetting = (FontSetting) resultFontSetting;
            }
            if (fontSize.GetValue(uiScreenMode, uiFineTuningMode, out var resultFontSize))
            {
                text.fontSize = resultFontSize;
            }
            if (overflowMode.GetValue(uiScreenMode, uiFineTuningMode, out var resultFontOverflowMode))
            {
                text.overflowMode = resultFontOverflowMode;
            }
        }
        
#endif
        
        void OnDestroy()
        {
            ScreenUIAdapterMgr.instance.RemoveAdapter(this);
        }
    }
}
